namespace eval cmd {}

proc cmd::add {comand wr} {
variable comands
set comands($comand) $wr
}

proc cmd::remove {comand} {
    variable $comand
	unset $comand
}


proc cmd::run {xlib from id type is_subject subject body err thread priority x} {
	if {$is_subject} {return}
	if {$err=="error"} {return}
	set txt [split $body " "]
	set comand [string tolower [lindex $txt 0]]
	set body [join [lrange $txt 1 [llength $txt]]]
	debugmsg cmd:run "BODY=$body"
	    variable comands
		set tmp "comands($comand)"
	debugmsg cmd:run $tmp
		if {![info exists [subst "$tmp"]]} {
			debugmsg cmd:run "command $comand not exists"
			return
		}
	set wr [subst "\$$tmp"]
	debugmsg cmd:run "WR=$wr"
	set code [catch { eval $wr [list $xlib $from $type $body $err $thread $priority $x ]} state]
        debugmsg comand ": $wr -> $state (code $code)"
	if {$code == 1} {
	    ::bgerror [format "Comand %s failed\nProcedure %s returned code\
			       %s\n\n%s" $comand $wr $code $state]
	}
}
hook::add message cmd::run 30